package com.example.smsbox;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

import com.example.adapter.ContactAdapter;
import com.example.callback.OnDataReceiver;
import com.example.database.DatabaseHelper;
import com.example.handler.ContactHandler;
import com.example.item.ItemList;

public class ContactActivity extends Activity implements OnDataReceiver {
	private ListView listview;
	private Handler handler;
	private ContactAdapter cadapter;
	private ContactHandler contacthandler;
	private Button btnAdd;
	private Button btnDelete;
	private final int REQ_ADD = 1;
	//DatabaseHelper db;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_contact);
		btnAdd = (Button) findViewById(R.id.btnAdd);
		btnDelete = (Button) findViewById(R.id.btnDelete);
		btnAdd.setOnClickListener(OnClickAdd);
		//db = new DatabaseHelper(this);
		Context context = getApplicationContext();
		contacthandler = new ContactHandler(context);
		contacthandler.setOnDataReceiverListener(ContactActivity.this);
		contacthandler.getAllContact();
		listview = (ListView) findViewById(R.id.listView1);
		listview.setOnItemClickListener(clickContactItem);
		handler = new Handler();

		cadapter = new ContactAdapter(getApplicationContext(), 1,
				new ArrayList<ItemList>());
		listview.setAdapter(cadapter);
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	private OnClickListener OnClickAdd = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			Intent intent = new Intent(ContactActivity.this, AddContactActivity.class);
			//startActivity(intent);
			startActivityForResult(intent, REQ_ADD);
		}
	};
	
	
	private OnItemClickListener clickContactItem = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			ItemList item = cadapter.getItem(position);
			String sdt = item.getSodienthoai();
			Log.i("So dien thoai = ", "" + item.getHoten());
//			Intent intent = new Intent(ContactActivity.this, ChatActivity.class);
//			intent.putExtra("sdt", sdt);
//			startActivity(intent);
		}
		
	};

	@Override
	public synchronized void OnDataContact(final ArrayList<ItemList> contact) {
		handler.post(new Runnable() {

			@Override
			public void run() {
				for (ItemList ct : contact) {
					cadapter.add(ct);
				}
				cadapter.notifyDataSetChanged();
			}
		});

	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode==RESULT_OK){
			if(requestCode==REQ_ADD){
				cadapter.clear();
				contacthandler.getAllContact();
			}
		}
	}
}
